#include <stdio.h>
#include <limits.h>

static const int V = 200;
int G[V][V];
bool used[V];

void init(int n)
{
	for (int i = 0; i < n; ++i)
	{
		used[i] = false;
		for (int j = 0; j < n; ++j)
			if (i == j)
				G[i][j] = 0;
			else
				G[i][j] = INT_MAX;
	}
}

int Dijkstra(int n, int s, int t)
{
	int* dist = G[s];
	int c = n;
	while (--c)
	{
		int min = INT_MAX;
		int add;
		for (int i = 0; i < n; ++i)
		{
			if (!used[i] && dist[i] < min)
			{
				add = i;
				min = dist[i];
			}
		}

		if (min == INT_MAX)
			break;

		used[add] = true;
		for (int i = 0; i < n; ++i)
		{
			if (!used[i] && G[add][i] != INT_MAX)
			{
				int d = dist[add] + G[add][i];
				if (dist[i] > d)
					dist[i] = d;
			}
		}
	}
	if (dist[t] == INT_MAX)
		return -1;
	else
		return dist[t];
}

int main()
{
	int n, m;
	while (scanf ("%d %d", &n, &m) != EOF)
	{
		init (n);
		while (m--)
		{
			int a, b, x;
			scanf ("%d %d %d", &a, &b, &x);
			if (G[a][b] > x)
				G[a][b] = G[b][a] = x;
		}
		int s, t;
		scanf ("%d %d", &s, &t);
		printf ("%d\n", Dijkstra(n, s, t));
	}
	return 0;
}